#include<string>
#include<unordered_map>
using namespace std;

class Solution {
public:
    string entityParser(string text) {
        unordered_map<string, string>helper_map;
        helper_map["&quot;"] = "\"";
        helper_map["&apos;"] = "'";
        helper_map["&amp;"] = "&";
        helper_map["&gt;"] = ">";
        helper_map["&lt;"] = "<";
        helper_map["&frasl;"] = "/";


        int OpIndex = -1;
        int lastIndex = 0;
        string res = "";
        for (int i = 0; i < text.size(); ++i) {
            switch (text[i]) {
            case '&':
                res += text.substr(lastIndex, i - lastIndex);
                lastIndex = i;
                OpIndex = i;
                break;
            case ';':
                if (OpIndex != -1) {
                    string op = text.substr(OpIndex, i - OpIndex + 1);
                    if (helper_map.find(op) != helper_map.end()) {
                        res += text.substr(lastIndex, OpIndex - lastIndex);
                        res += helper_map[op];
                    }
                    else {
                        res += text.substr(lastIndex, i - lastIndex + 1);
                    }
                    lastIndex = i + 1;
                    OpIndex = -1;
                }
                break;
            default:

                break;
            }
        }

        if (lastIndex < text.size())
            res += text.substr(lastIndex);
        return res;
    }
};